
import { Button, Modal } from 'antd';
import { inject, observer } from "mobx-react";
import { toJS } from 'mobx';
import { WeaDialog, WeaFormItem, WeaSearchGroup, WeaNewScroll, WeaRightMenu, WeaTab } from 'ecCom';
import { WeaSwitch, WeaTableNew } from 'comsMobx';
import CptInventoryPlanForm from '../form/CptInventoryPlanForm';
import PlanListInfoDialog from "./PlanListInfoDialog"
import ListImport from "./InventoryListImport"

const WeaTable = WeaTableNew.WeaTable;
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;


@observer
class PlanInfoDialog extends React.Component {
    constructor(props) {
        super(props);
    }
    componentDidMount() {
    }
    componentWillReceiveProps(nextProps) {
    }

    render() {
        const {doAddlist, addlistVisible, editlistVisible, editlistform, editlistformCondition,
            setVisiable, addlistform, addlistformCondition, sessionkey_plan, plantableStore, doEditlist ,selectid,importVisible,reflesh,handleDialog_imp,doImport} = this.props.contentStore;       

        let addButtons = [
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@fyry7r@save`} type="primary" onClick={() => doAddlist()} >{getLabel(86, "保存")}</Button>),
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@rhywnc@close`} type="ghost" onClick={() => this.closeAllDialog(3)} >{getLabel(201, "取消")}</Button>)
        ]
        let editButtons = [
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@gagn11@save`} type="primary" onClick={() => doEditlist()} >{getLabel(86, "保存")}</Button>),
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@9jp33t@close`} type="ghost" onClick={() => this.closeAllDialog(4)} >{getLabel(201, "取消")}</Button>)
        ]
        let dialogHeight = window.innerHeight;
        if (dialogHeight > 768) {
            dialogHeight = 600;
        }else{
            dialogHeight = 500;
        }

        return (
            <div style={{ height: '100%' }}>
                <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@eyng1k`} datas={this.getRightMenu()} >
                    <div className='cpt-dialog-form'>
                        {this.getFormFields()}
                    </div>
                    <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@9r9t8n`}
                        sessionkey={sessionkey_plan}
                        comsWeaTableStore={plantableStore}
                        hasOrder={true}
                        needScroll={true}
                        outerHeight={dialogHeight-50}
                        onOperatesClick={this.onOperatesClick}
                        getColumns={c => this.reRenderColumns(c)}
                    />
                </WeaRightMenu>

                <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@mm681y`}
                    title={getLabel('384641', "新建盘点表")}
                    visible={addlistVisible}
                    onCancel={() => { setVisiable(false, 3) }}
                    icon='icon-coms-fa'
                    iconBgcolor='#f14a2d'
                    buttons={addButtons}
                >
                    <CptInventoryPlanForm ecId={`${this && this.props && this.props.ecId || ''}_CptInventoryPlanForm@z2fy0x`} form={addlistform} formcondition={addlistformCondition} method="add" />
                </WeaDialog>

                <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@c4kvyo`}
                    title={getLabel('384642', "编辑盘点表")}
                    visible={editlistVisible}
                    onCancel={() => { setVisiable(false, 4) }}
                    icon='icon-coms-fa'
                    iconBgcolor='#f14a2d'
                    buttons={editButtons}
                >
                    <CptInventoryPlanForm ecId={`${this && this.props && this.props.ecId || ''}_CptInventoryPlanForm@tc9zvq`} form={editlistform} formcondition={editlistformCondition} method="edit" />
                </WeaDialog>
                <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@bk9tty`}
                    title={getLabel(1506, "盘点") + getLabel(18596, "导入")}
                    visible={importVisible}
                    onCancel={() => {handleDialog_imp(false)}}
                    icon='icon-coms-fa'
                    iconBgcolor='#f14a2d'
                    buttons={[
                        (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@6dddn9@submit`} type="primary" onClick={() => doImport()} >{getLabel(615, "提交")}</Button>),
                        (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@fp3kc6@close`} type="ghost" onClick={() =>handleDialog_imp(false) } >{getLabel(309, "关闭")}</Button>)
                    ]}
                    style={{ width: 750, height: 400 }}
                >
                    <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@6mexil`} height={"100%"}>
                        <ListImport ecId={`${this && this.props && this.props.ecId || ''}_ListImport@9v93z2`} contentStore={this.props.contentStore} key={reflesh}  />
                    </WeaNewScroll >
                </WeaDialog>

                {/* 计划明细 */}
                <PlanListInfoDialog ecId={`${this && this.props && this.props.ecId || ''}_PlanListInfoDialog@k7fdzs`} contentStore={this.props.contentStore} />

            </div>)
    }
    getRightMenu() {
        const { setVisiable, plantableStore, canEditlist,inventorystate, selectid,handleDialog_imp } = this.props.contentStore;
        const selectedLen = plantableStore.selectedRowKeys.length;
        let btns = [];
        if (canEditlist) {
            btns.push({
                key: "create",
                icon: <i className='icon-coms-New-Flow' />,
                content: getLabel(82, '新建'),
                onClick: key => {
                    this.props.contentStore.getAddPlanlistForm({ id: selectid });
                    const { addlistVisible, setVisiable } = this.props.contentStore;
                    if (!addlistVisible) {
                        setVisiable(true, 3)
                    }
                }
            });
            if(inventorystate=='0'){
                btns.push({
                    key: "batchImp",
                    icon: <i className='icon-coms-New-Flow' />,
                    content: getLabel(26601, '批量导入'),
                    onClick: key => {
                        handleDialog_imp(true);
                    }
                });
            }
            btns.push({
                key: "batchDelete",
                disabled: selectedLen == 0,
                icon: <i className='icon-coms-delete' />,
                content: getLabel(32136, '批量删除'),
                onClick: key => {
                    this.doDelete();
                }
            });

        }

        return btns;
    }

    doDelete() {
        const { plantableStore } = this.props.contentStore;
        const { selectedRowKeys } = plantableStore;//得到选中数据的id
        const count = toJS(selectedRowKeys).length;
        // alert(selectedRowKeys)
        let contentStore = this.props.contentStore;
        Modal.confirm({
            title: getLabel(15172, '系统提示'),
            content: getLabel(83600, '您确认要删除吗?'),
            onOk() {
                //this.props.contentStore.doDeleteList(selectids); 这个在里面无法使用props已经失效
                contentStore.doDeleteList(selectedRowKeys);
            },
            onCancel() { },
        });
    }

    closeAllDialog(params) {
        const { setVisiable } = this.props.contentStore;
        if (params == 3) {
            setVisiable(false, 3);
        } else if (params == 4) {
            setVisiable(false, 4);
        }

    }

    getFormFields() {
        const { contentStore } = this.props;
        const { basicInfo, planlistForm } = contentStore;
        const { isFormInit } = planlistForm;
        let group = [];
        //  const formParams = instockForm.getFormParams(); 
        isFormInit && basicInfo.map((c,i) => {
            let items = [];
            c.items.map(fields => {
                let hide = false;
                let hasunderline = false;
                //只读样式控制
                if (fields.viewAttr == "1" || (fields.conditionType == "BROWSER" && fields.browserConditionParam.viewAttr == "1")) {
                    hasunderline = true
                }
                if (hasunderline) {
                    items.push({
                        com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@hos8jh@${fields.domkey[0]}`}
                            label={`${fields.label}`}
                            labelCol={{ span: `${fields.labelcol}` }}
                            wrapperCol={{ span: `${fields.fieldcol}` }} underline>
                            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@i5xmyn@${fields.domkey[0]}`} fieldConfig={fields} form={planlistForm} />
                        </WeaFormItem>),
                        colSpan: 1,
                        hide: hide
                    });
                } else {
                    items.push({
                        com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@d5vr0n@${fields.domkey[0]}`}
                            label={`${fields.label}`}
                            labelCol={{ span: `${fields.labelcol}` }}
                            wrapperCol={{ span: `${fields.fieldcol}` }} >
                            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@nazqh8@${fields.domkey[0]}`} fieldConfig={fields} form={planlistForm} />
                        </WeaFormItem>),
                        colSpan: 1,
                        hide: hide
                    });
                }

            });
            group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@e8cal1@${i}`} needTigger={true} title={c.title} col={2} showGroup={c.defaultshow} items={items} />)
        });
        return group;
    }

    editTableDatas(datas) {
        const { contentStore } = this.props;
        contentStore.saveSubListData(datas);
    }

    onOperatesClick = (record, index, operate, flag, argumentString) => {
        const { setSelectId, setVisiable, getEditPlanlistForm, doDeleteList, doStartList,handlePlanList, setListId, cptform } = this.props.contentStore;
        let _href = operate && operate.href ? operate.href : "";
        let fn = _href.replace("javascript:", "");
        fn = fn.substring(0, fn.indexOf('('));
        let that = this;
        if (fn != "") {
            if ("onEdit" == fn) {
                // setSelectId({ selectid: record.randomFieldId });
                setVisiable(true, 4);
                getEditPlanlistForm({
                    id: record.randomFieldId
                });
            } else if ("onDel" == fn) {
                Modal.confirm({
                    title: getLabel(15172, '系统提示'),
                    content: getLabel(83600, '您确认要删除吗?'),
                    onOk() {
                        doDeleteList(record.randomFieldId);
                    },
                    onCancel() { },
                });
            } else if ("onViewDetail" == fn) {
                //setSelectId({ selectid: record.randomFieldId });
                setListId({ listid: record.randomFieldId });
                cptform.resetConditionValue(); //清除查询条件值
                handlePlanList(true, { id: record.randomFieldId, name: record.name }, 0);
            } else if ("onAdd" == fn) {
                setListId({ listid: record.randomFieldId });
                cptform.resetConditionValue(); //清除查询条件值
                handlePlanList(true, { id: record.randomFieldId, name: record.name }, 0)
            }else if ("onStart" == fn) {
                doStartList(record.randomFieldId);
            }
        }
    }

    reRenderColumns(columns) {
        const { handlePlanList, setListId, cptform } = this.props.contentStore;
        var divStyle = {
            cursor: 'pointer'
        };
        columns.forEach(c => {
            if (c.dataIndex == 'mark' || c.dataIndex == 'name') {
                c.render = function (text, record) {
                    let valueSpan = record[c.dataIndex + "span"] !== undefined ? record[c.dataIndex + "span"] : record[c.dataIndex];
                    return <span style={divStyle} onClick={() => {
                        setListId({ listid: record.randomFieldId });
                        cptform.resetConditionValue(); //清除查询条件值
                        handlePlanList(true, { id: record.randomFieldId, name: record.name }, 0)
                    }}
                        dangerouslySetInnerHTML={{ __html: valueSpan }}>
                    </span>
                }
            }
        })
        return columns;
    }


}

export default PlanInfoDialog;